﻿namespace HiMap.MapControls
{
    using HiMap.Carto;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class LayerControl : UserControl
    {
        private MapControl _Map;
        private IContainer components;
        private ImageList img;
        private ImageList imgOld;
        private bool NodeRef;
        private TreeNode oldNode;
        private TreeView treeLayer;

        public event TreeViewEventHandler LayerAfterSelect;

        public LayerControl()
        {
            this.InitializeComponent();
            this._Map = null;
            this.NodeRef = true;
            this.oldNode = null;
            Graphics graphics = Graphics.FromImage(this.img.Images[0]);
            SizeF ef = graphics.MeasureString("test", this.treeLayer.Font);
            this.img.ImageSize = new Size(Convert.ToInt32(ef.Height), Convert.ToInt32(ef.Height));
            this.imgOld.ImageSize = new Size(Convert.ToInt32(ef.Height), Convert.ToInt32(ef.Height));
            graphics.Dispose();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public ILayer GetLayer()
        {
            HiMap.Carto.Map map = this.Map.Map;
            ILayer layer = null;
            if ((this.treeLayer.SelectedNode != null) && (this.treeLayer.SelectedNode.Tag.ToString() == "Layer"))
            {
                layer = map.GetLayer(this.treeLayer.SelectedNode.Index);
            }
            return layer;
        }

        protected internal Image GetNodeImage(TreeNode Node)
        {
            return this.img.Images[Node.ImageIndex];
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(LayerControl));
            this.treeLayer = new TreeView();
            this.img = new ImageList();
            this.imgOld = new ImageList();
            base.SuspendLayout();
            this.treeLayer.CheckBoxes = true;
            this.treeLayer.Dock = DockStyle.Fill;
            this.treeLayer.ImageIndex = 0;
            this.treeLayer.ImageList = this.img;
            this.treeLayer.Location = new Point(0, 0);
            this.treeLayer.Name = "treeLayer";
            this.treeLayer.SelectedImageIndex = 0;
            this.treeLayer.Size = new Size(150, 0xfb);
            this.treeLayer.TabIndex = 0;
            this.treeLayer.AfterCheck += new TreeViewEventHandler(this.treeLayer_AfterCheck);
            this.treeLayer.AfterSelect += new TreeViewEventHandler(this.treeLayer_AfterSelect);
            this.treeLayer.AfterExpand += new TreeViewEventHandler(this.treeLayer_AfterExpand);
            this.img.Images.Clear();
            this.img.Images.Add((Image) manager.GetObject("resource"));
            this.img.Images.Add((Image) manager.GetObject("resource1"));
            this.imgOld.Images.Clear();
            this.imgOld.Images.Add((Image) manager.GetObject("resource2"));
            this.imgOld.Images.Add((Image) manager.GetObject("resource3"));
            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;
            base.Controls.Add(this.treeLayer);
            base.Name = "LayerControl";
            base.Size = new Size(150, 0xfb);
            base.ResumeLayout(false);
        }

        private void OnLayerChanged(object sender, EventArgs e)
        {
            this.treeLayer.SelectedNode = null;
            this.SetLayer();
        }

        private void OnMapChanged(object sender, EventArgs e)
        {
            this.treeLayer.SelectedNode = null;
            this.SetLayer();
        }

        private void OnStyleChanged(object sender, EventArgs e)
        {
        }

        private void OpenStyle(TreeNode node)
        {
            HiMap.Carto.Map map = this.Map.Map;
            if (node.Tag.ToString() == "Color")
            {
                IFeatureLayer layer = (IFeatureLayer) map.GetLayer(node.Parent.Index);
            }
        }

        public override void Refresh()
        {
            base.Refresh();
            this.Map = this._Map;
        }

        private void SetChNode(TreeNode Node, ILayer lyr)
        {
            this.img.Images.Clear();
            this.img.Images.Add(this.imgOld.Images[0]);
            this.img.Images.Add(this.imgOld.Images[1]);
            LayerType type = lyr.Type;
        }

        private void SetLayer()
        {
            HiMap.Carto.Map map = this.Map.Map;
            if (map != null)
            {
                this.treeLayer.Nodes.Clear();
                TreeNode node = new TreeNode(map.MapName);
                node.Tag = "Layers";
                node.ImageIndex = 0;
                node.SelectedImageIndex = 0;
                node.Checked = true;
                this.treeLayer.Nodes.Add(node);
                for (int i = 0; i < map.LayerCount; i++)
                {
                    TreeNode node2 = new TreeNode();
                    ILayer lyr = map.GetLayer(i);
                    node2.Text = lyr.Caption;
                    node2.Tag = "Layer";
                    node2.ImageIndex = 1;
                    node2.SelectedImageIndex = 1;
                    if (lyr.Visible)
                    {
                        node2.Checked = true;
                    }
                    else
                    {
                        node2.Checked = false;
                    }
                    this.SetChNode(node2, lyr);
                    node.Nodes.Add(node2);
                }
            }
        }

        public void SetLayer(int index)
        {
            if (this.treeLayer.Nodes.Count != 0)
            {
                TreeNode node = this.treeLayer.Nodes[0];
                if (node.Nodes.Count >= index)
                {
                    this.treeLayer.SelectedNode = node.Nodes[index];
                }
            }
        }

        private void treeLayer_AfterCheck(object sender, TreeViewEventArgs e)
        {
            HiMap.Carto.Map map = this.Map.Map;
            string text = e.Node.Tag.ToString();
            if (text != null)
            {
                if (text != "Layer")
                {
                    if (text != "Layers")
                    {
                        return;
                    }
                }
                else
                {
                    map.GetLayer(e.Node.Index).Visible = e.Node.Checked;
                    this.Map.MapRefresh();
                    if (e.Node.Checked)
                    {
                        this.NodeRef = false;
                        e.Node.Parent.Checked = true;
                    }
                    return;
                }
                if (this.NodeRef)
                {
                    for (int i = 0; i < map.LayerCount; i++)
                    {
                        e.Node.Nodes[i].Checked = e.Node.Checked;
                    }
                }
                this.NodeRef = true;
            }
        }

        private void treeLayer_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if ((e.Node != null) && (e.Node.Tag.ToString() == "Layer"))
            {
                if (this.oldNode != null)
                {
                    this.oldNode.Collapse();
                }
                e.Node.Nodes.Clear();
                ILayer lyr = this.Map.Map.GetLayer(e.Node.Index);
                this.SetChNode(e.Node, lyr);
                this.oldNode = e.Node;
            }
        }

        private void treeLayer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeLayer.SelectedNode != null)
            {
                this.GetLayer();
                this.LayerAfterSelect(sender, e);
                this.OpenStyle(e.Node);
            }
        }

        public MapControl Map
        {
            get
            {
                return this._Map;
            }
            set
            {
                this._Map = value;
                this.SetLayer();
                if (this._Map.Map != null)
                {
                    this._Map.Map.LayerChanged += new EventHandler(this.OnLayerChanged);
                }
                if (this._Map != null)
                {
                    this._Map.MapChanged += new EventHandler(this.OnMapChanged);
                }
            }
        }
    }
}

